package com.example.homework.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class UserDbHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "user.db";
    private static final int DATABASE_VERSION = 1;

    public static final String TABLE_NAME = "users";
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_PASSWORD = "password";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_SEX = "sex";

    public UserDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableSql = "CREATE TABLE " + TABLE_NAME + "("
                + COLUMN_USERNAME + " TEXT PRIMARY KEY,"
                + COLUMN_PASSWORD + " TEXT,"
                + COLUMN_NAME + " TEXT,"
                + COLUMN_SEX + " TEXT"
                + ")";
        try {
            db.execSQL(createTableSql);
            Log.d("UserDbHelper", "用户表创建成功");
        } catch (Exception e) {
            Log.e("UserDbHelper", "创建用户表出现异常: " + e.getMessage());
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            // 示例：假设从版本1升级到版本2时，添加一个新字段 'phone_number'
            if (oldVersion == 1 && newVersion == 2) {
                try {
                    String alterTableSql = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN phone_number TEXT";
                    db.execSQL(alterTableSql);
                    Log.d("UserDbHelper", "表结构升级成功，添加phone_number字段");
                } catch (Exception e) {
                    Log.e("UserDbHelper", "表结构升级出现异常: " + e.getMessage());
                }
            }
            // 可以根据更多不同版本升级情况继续添加相应的逻辑
        }
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param username 要查询的用户名
     * @return Cursor对象，包含查询到的用户信息，使用后需要手动关闭
     */
    public Cursor getUserByUsername(String username) {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] columns = {COLUMN_USERNAME, COLUMN_PASSWORD, COLUMN_NAME, COLUMN_SEX};
        String selection = COLUMN_USERNAME + " =?";
        String[] selectionArgs = {username};
        return db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
    }

    /**
     * 插入用户信息到数据库
     *
     * @param username 用户名
     * @param password 密码
     * @param name     姓名
     * @param sex      性别
     * @return 插入成功返回行 ID，否则返回 -1
     */
    public long insertUser(String username, String password, String name, String sex) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_USERNAME, username);
        values.put(COLUMN_PASSWORD, password);
        values.put(COLUMN_NAME, name);
        values.put(COLUMN_SEX, sex);
        long result = db.insert(TABLE_NAME, null, values);
        db.close();
        return result;
    }

    /**
     * 更新用户信息
     *
     * @param username  用户名
     * @param newValues 包含要更新的字段和对应新值的ContentValues对象
     * @return 更新成功返回受影响的行数，否则返回 0
     */
    public int updateUser(String username, ContentValues newValues) {
        SQLiteDatabase db = this.getWritableDatabase();
        String selection = COLUMN_USERNAME + " =?";
        String[] selectionArgs = {username};
        int result = db.update(TABLE_NAME, newValues, selection, selectionArgs);
        db.close();
        return result;
    }

    /**
     * 根据用户名删除用户信息
     *
     * @param username 要删除的用户名
     * @return 删除成功返回受影响的行数，否则返回 0
     */
    public int deleteUser(String username) {
        SQLiteDatabase db = this.getWritableDatabase();
        String selection = COLUMN_USERNAME + " =?";
        String[] selectionArgs = {username};
        int result = db.delete(TABLE_NAME, selection, selectionArgs);
        db.close();
        return result;
    }
}